package com.sky.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.sky.mapper.model.User;

@Aspect//切面类
@Component//交给spring托管
public class AopUtil {
	

	@Pointcut(value="execution (* com.sky.service.impl.*.*(..))")
	public void	 cutPoint(){
		
	}
	@Before(value="cutPoint()")
	public void	 before(){
		System.out.println("注解aop前开启"+"=---------====");
	}
	@After(value="cutPoint()")
	public void	 after(){
		System.out.println("注解aop后"+"======");
	}
	
	
	@Around(value="cutPoint()")
	public Object around(ProceedingJoinPoint pjp){
		System.out.println("around=====1");
		Object result=null;
		try {
			//类似动态代理在它前后植入代码块（例如@before）
		String methodname  = 	pjp.getSignature().getName();
	Object[] args = pjp.getArgs();
	User u =(User) args[0].getClass().newInstance();
	u.setUname("dfdf");
	for (Object object : args) {
		System.out.println(object);
	}
		System.out.println(methodname);	
			result = pjp.proceed();
			System.out.println("around----------2");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@AfterThrowing(value="cutPoint()",throwing="e")
	public void	 after(Exception e){
		System.out.println("注解aop后"+"==------===");
	}
	
	
}
